/**
 * 
 */
package sort.ui.components;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import sort.result.SortResult;

/**
 * @author masaru-gotou
 *
 */
@SuppressWarnings("serial")
public class ResultTableModel extends AbstractTableModel {
    private static final String[] columnNames = {
		"No",
		"Algorithm",
        "records",
        "exec count",
        "avg time",
        "avg mem usage",
        "Identity",
        "Stability"
        };
    private List<SortResult> results = new ArrayList<SortResult>();

	public ResultTableModel() {
	}
	public ResultTableModel(List<SortResult> results) {
		this();
		this.addAll(results);
	}
	public void add(SortResult result) {
		int before = results.size();
		this.results.add(result);
		fireTableRowsInserted(before, results.size());
	}
	public void addAll(List<SortResult> results) {
		int before = results.size();
		this.results.addAll(results);
		fireTableRowsInserted(before, results.size());
	}
	public void clear() {
		int before = results.size();
		results.clear();
		fireTableRowsDeleted(0, before);
	}
	@Override
	public String getColumnName(int colIndex) {
		return columnNames[colIndex];
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getColumnCount()
	 */
	@Override
	public int getColumnCount() {
		return columnNames.length;
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getRowCount()
	 */
	@Override
	public int getRowCount() {
		return results.size();
	}

	/* (non-Javadoc)
	 * @see javax.swing.table.TableModel#getValueAt(int, int)
	 */
	@Override
	public Object getValueAt(int rowIndex, int colIndex) {
		SortResult result = results.get(rowIndex);
		if ( colIndex == 0 ) return rowIndex+1;
		if ( colIndex == 1 ) return result.getAlgorithmName();
		if ( colIndex == 2 ) return result.getRecordCount();
		if ( colIndex == 3 ) return result.getElapsedTime();
		if ( colIndex == 4 ) return result.getUsedMemory();
		if ( colIndex == 5 ) return result.isSorted();
		if ( colIndex == 6 ) return result.isStabled();
		
		return null;
	}

}
